﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Yunzuan.Foundation.Log;
using YunZuan.Models;

namespace Yunzuan.Common.Log
{
    public class CommonLog
    {
        public static string _logTemplate = "方法名：{0}，参数名：{1}，行号：{2} ,参数值：{3}，栈信息:{4}";
        /// <summary>
        /// {{\"msgcode\":{0},\"msginfo\":\"{1}\"
        /// </summary>
        static readonly string msgInfo = "{{\"msgcode\":{0},\"msginfo\":\"{1}\"}}";
        /// <summary>
        /// "{{\"msgcode\":{0},\"msginfo\":{1}}}
        /// </summary>
        static readonly string msgObj = "{{\"msgcode\":{0},\"msginfo\":{1}}}";

        //上传图片返回的格式

        public static string showCount = System.Configuration.ConfigurationManager.AppSettings["InventoryCount"];  //后端控制显示多少条，因为是敏感数据
               
   
        /// <summary>
        /// 添加后返回对应Id
        /// </summary>
        public static readonly string msgInfoAddReturnIdJson = "{{\"msgcode\":{0},\"msginfo\":\"{1}\",\"Id\":\"{2}\"}}";

        
        #region 全局日志
        public static SearchParamLog log = new SearchParamLog(); 
        /// 得到当前方法并记录日志
        /// </summary>
        /// <param name="ex"></param>
        public static void GetMethodInfoAndWriteLogs(Type type, Exception ex = null, string msg = "")
        { 
            try
            {
                StackTrace st = new StackTrace(true);
                MethodBase mb = st.GetFrame(1).GetMethod();
                var line = "";
                var errInfo = "";
                var paramName0 = "";//记录第一个参数名
                if (ex != null && ex.StackTrace.IndexOf("行号") > -1) //有些错误信息没有行号，导致什么信息都没有记录
                {
                    line = ex.StackTrace.Substring(ex.StackTrace.IndexOf("行号"), ex.StackTrace.Length - ex.StackTrace.IndexOf("行号")); //行号
                    errInfo = ex.Message;
                }
                //取得父参数名
                if (mb.GetParameters().Count() > 0)
                {
                    paramName0 = mb.GetParameters()[0].Name;
                }
                var logTemplate = string.Format(CommonConst._logTemplate, mb.Name, paramName0, line, CommonConst.logParam);
                if (msg != "")
                {
                    Logger.Info(type, logTemplate + "-记录信息：" + msg);
                }
                else
                    Logger.Error(type, logTemplate + "-错误信息：" + errInfo);
            }
            catch (Exception e)
            {
                Logger.Error(type, CommonConst.logParam + "-写日志出错信息：" + e.Message + ",e.StackTrace:" + e.StackTrace);
            }
        }


        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="tttagKey"></param>
        /// <param name="messeage"></param>
        public static void SendMQMsg(string tttagKey, string messeage)
        {
            Msg msgS = new Msg();
            msgS.TagKey = tttagKey;
            msgS.Desc = messeage;
            ConmonMQMsgClass.send(msgS);
        }
        /// <summary>
        /// 正确错误都可以用，两个参数
        /// </summary>
        /// <param name="b">成功传true ,失败 false</param>
        /// <param name="msg">提示的信息</param>
        /// <returns></returns>
        public static string MsgInfo(bool b, string msg) 
        {
            return string.Format(msgInfo, b.ToString().ToLower(), msg);
        }
        /// <summary>
        /// 正确错误都可以用，两个参数
        /// </summary>
        /// <param name="b">成功传true ,失败 false</param>
        /// <param name="msg">提示的对象</param>
        /// <returns></returns>
        public static string MsgObj(bool b, object obj)
        {
            return string.Format(msgObj, b.ToString().ToLower(), obj);
        }

        /// <summary>
        /// 成功的信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string MsgSuccessInfo(string msg)
        {
            return MsgInfo(true, msg);
        }
        /// <summary>
        /// 错误的信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string MsgErrorInfo(string msg)
        { 
            return MsgInfo(false, msg);
        }
        /// <summary>
        /// 成功的信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string MsgSuccessObj(object obj)
        {
            return MsgObj(true, obj);
        }
        /// <summary>
        /// 错误的信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string MsgErrorObj(string msg)
        {
            return MsgObj(false, msg);
        }

    }

    #endregion 日志
 

    public class Msg
    {
        public string TagKey { get; set; }
        public string Desc { get; set; } 
    }
}
